package com.quectel.sys.module.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.sys.constants.RedisCacheConstants;
import com.quectel.sys.module.sys.dao.SmallFileDao;
import com.quectel.sys.module.sys.dto.SmallFileDto;
import com.quectel.sys.module.sys.entity.SmallFileEntity;
import com.quectel.sys.module.sys.service.SmallFileService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/09/20 13:11
 */
@DubboService
public class SmallFileServiceImpl implements SmallFileService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmallFileServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SMALL_FILE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SMALL_FILE_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SmallFileDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SMALL_FILE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SmallFileDao smallFileDao;


    @Override
    public SmallFileDto selectBySha1Code(Long tenantId, String fileSha1Hex) {
        SmallFileEntity smallFileEntity = smallFileDao.selectOne(new LambdaQueryWrapper<SmallFileEntity>()
                .eq(SmallFileEntity::getTenantId, tenantId)
                .eq(SmallFileEntity::getSha1Code, fileSha1Hex)
                .last(SqlConstants.MYSQL_LIMIT_ONE).select(SmallFileEntity::getId));
        return CopyUtils.copyObj(smallFileEntity, SmallFileDto.class);
    }

    @Override
    public SmallFileDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SMALL_FILE_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SmallFileDto selectById(Long id) {
        SmallFileEntity entity = smallFileDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, SmallFileDto.class));
    }

    @Override
    public List<SmallFileDto> queryList(Map<String, Object> params) {
        List<SmallFileEntity> list = smallFileDao.queryList(params);
        List<SmallFileDto> result = CopyUtils.copyList(list, SmallFileDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属社区等
     *
     * @param dto
     * @return
     */
    private SmallFileDto paddingField(SmallFileDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return smallFileDao.queryTotal(params);
    }

    @Override
    public Long save(SmallFileDto dto) {
        SmallFileEntity entity = CopyUtils.copyObj(dto, SmallFileEntity.class);
        entity.setId(Snowflake.nextId());
        smallFileDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SmallFileDto dto) {
        SmallFileEntity entity = CopyUtils.copyObj(dto, SmallFileEntity.class);

        smallFileDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SmallFileDto dto) {
        SmallFileEntity entity = CopyUtils.copyObj(dto, SmallFileEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, smallFileDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        smallFileDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
